package com.miciyun.springbootrabbitmqdemo.demos.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.amqp.core.Queue;

import static com.miciyun.springbootrabbitmqdemo.demos.constant.RabbitTopicApiEnum.EXCHANGE_NAME;
import static com.miciyun.springbootrabbitmqdemo.demos.constant.RabbitTopicApiEnum.NOTICE_MSG_ROUTE_KEY;

/**
 * @author mctec
 */
@Configuration
public class RabbitTopicConfig {
    @Bean
    public Queue noticMsgQueue(){
        return new Queue("notice.msg", true);
    }

    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(EXCHANGE_NAME.content(), true, false);
    }

    //绑定  将队列和交换机绑定, 并设置用于匹配键：api.pbrmyy.notice.msg.*
    @Bean
    public Binding bindingDirect1() {
        return BindingBuilder.bind(noticMsgQueue()).to(topicExchange()).with(NOTICE_MSG_ROUTE_KEY.content() + ".*");
    }

    // 对象序列化
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }
}
